package com.example.neutonote2.data.repository;

import android.app.Application;
import androidx.lifecycle.LiveData;
import com.example.neutonote2.data.dao.TagDao;
import com.example.neutonote2.data.db.NeuToNoteDatabase;
import com.example.neutonote2.data.model.Tag;
import com.example.neutonote2.data.model.NoteTagCrossRef;

import java.util.List;

/**
 * 标签仓库类，用于处理标签数据操作
 */
public class TagRepository {
    private final TagDao tagDao;
    private final LiveData<List<Tag>> allTags;

    public TagRepository(Application application) {
        NeuToNoteDatabase db = NeuToNoteDatabase.getDatabase(application);
        tagDao = db.tagDao();
        allTags = tagDao.getAllTags();
    }

    // 获取所有标签
    public LiveData<List<Tag>> getAllTags() {
        return allTags;
    }

    // 按ID获取标签
    public LiveData<Tag> getTagById(int id) {
        return tagDao.getTagById(id);
    }
    
    // 获取笔记的所有标签
    public LiveData<List<Tag>> getTagsForNote(int noteId) {
        return tagDao.getTagsForNote(noteId);
    }

    // 插入标签
    public void insert(Tag tag) {
        NeuToNoteDatabase.databaseWriteExecutor.execute(() -> {
            tagDao.insert(tag);
        });
    }

    // 更新标签
    public void update(Tag tag) {
        NeuToNoteDatabase.databaseWriteExecutor.execute(() -> {
            tagDao.update(tag);
        });
    }

    // 删除标签
    public void delete(Tag tag) {
        NeuToNoteDatabase.databaseWriteExecutor.execute(() -> {
            tagDao.delete(tag);
        });
    }
    
    // 添加标签到笔记
    public void addTagToNote(int noteId, int tagId) {
        NeuToNoteDatabase.databaseWriteExecutor.execute(() -> {
            tagDao.insertNoteTagCrossRef(new NoteTagCrossRef(noteId, tagId));
        });
    }
    
    // 从笔记中移除标签
    public void removeTagFromNote(int noteId, int tagId) {
        NeuToNoteDatabase.databaseWriteExecutor.execute(() -> {
            tagDao.deleteNoteTagCrossRef(new NoteTagCrossRef(noteId, tagId));
        });
    }
    
    // 移除笔记的所有标签
    public void removeAllTagsFromNote(int noteId) {
        NeuToNoteDatabase.databaseWriteExecutor.execute(() -> {
            tagDao.deleteAllTagsForNote(noteId);
        });
    }
    
    /**
     * 清理无效标签（包括错误消息）
     */
    public void cleanInvalidTags() {
        NeuToNoteDatabase.databaseWriteExecutor.execute(() -> {
            List<Tag> allTags = tagDao.getAllTagsSync();
            if (allTags != null) {
                for (Tag tag : allTags) {
                    if (tag != null && tag.getName() != null) {
                        String name = tag.getName().toLowerCase();
                        // 检查是否是错误消息
                        if (name.contains("unable to resolve host") || 
                            name.contains("no address associated") ||
                            name.contains("failed to connect") ||
                            name.contains("exception") ||
                            name.contains("error") ||
                            name.contains("故障") ||
                            name.contains("失败") ||
                            name.contains("错误") ||
                            name.contains("异常") ||
                            name.contains("://") ||
                            name.length() > 20) {
                            // 删除无效标签
                            tagDao.delete(tag);
                        }
                    }
                }
            }
        });
    }
} 